package com.woniuxy.intellihome.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.intellihome.entity.Cart;
import com.woniuxy.intellihome.mapper.CartMapper;
import com.woniuxy.intellihome.service.CartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.intellihome.service.SkuService;
import com.woniuxy.intellihome.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-08-11
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {
    @Autowired
    private CartService cartService;
    @Autowired
    private UserService userService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private CartMapper cartMapper;

    /**
     * 完整cart
     * 设置完整user
     * 设置完整sku
     *
     * @param cartId cart id
     * @return cart
     */
    @Override
    public Cart getFullCart(int cartId) {
        Cart cart = cartService.getById(cartId);
        cart.setUser(userService.getById(cart.getUserId()));
        cart.setSku(skuService.getWithSpuSpecDetailBySKUId(cart.getSkuId()));
        return cart;
    }

    //得到完整的cart list
    @Override
    public List<Cart> getFullCartListByUserId(int userId) {
        Cart cart = new Cart();
        cart.setUserId(userId);
        Wrapper<Cart> wrapper = new QueryWrapper<>(cart);
        List<Cart> carts = cartService.list(wrapper);
        List<Cart> cartList = new ArrayList<>();
        for (Cart cart1 : carts) {
            Cart cartTemp = cartService.getFullCart(cart1.getId());
            cartList.add(cartTemp);
        }
        return cartList;
    }

    @Override
    public void add(Cart cart) {
        cartMapper.insert(cart);
    }
}
